﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class ModifyUser : Form
    {        
        private string LastIdUser;
        private string userName;
        private string password;
        private string pictureValid;

        private OpenFileDialog openFile = new OpenFileDialog();
        public ModifyUser(Manager user)
        {
            InitializeComponent();
            LoadUserModify(user);
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    //buttonSaveChange.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void LoadUserModify(Manager user)
        {
            labelDate.Text = Convert.ToString(user.DateInscription);
            textBoxCi.Text = user.CIManager;
            textBoxFirstName.Text = user.FirstName;
            textBoxLastName.Text = user.LastName;
            textBoxAddress.Text = user.Address;
            textBoxCellPhone.Text = user.CellPhone.ToString();
            textBoxPhone.Text = user.Phone.ToString();
            textBoxEmail.Text = user.Email;
            comboBoxGender.Text = user.Gender;
            //comboBoxUserType.Text = user.UserType;
            password = user.Password;
            userName = user.UserName;
            LastIdUser = user.CIManager;
            pictureValid = user.ImageManager;
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", user.ImageManager)));
                pictureBoxPicture.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxPicture.Image = picture;
            }            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();            
        }

        private void buttonSaveChange_Click(object sender, EventArgs e)
        {
            if (ValidatingFields())
            {                
                Manager user = new Manager();
                user.CIManager = textBoxCi.Text;
                user.FirstName = textBoxFirstName.Text;
                user.LastName = textBoxLastName.Text;
                user.Address = textBoxAddress.Text;
                user.CellPhone = int.Parse(textBoxCellPhone.Text);
                user.Phone = int.Parse(textBoxPhone.Text);
                user.Email = textBoxEmail.Text;
                user.Gender = comboBoxGender.Text;                
                user.Password = password;
                user.UserName = userName;
                user.StateManager = true;                
                user.ImageManager = pictureValid;

                DialogResult confirm = MessageBox.Show("Are you sure to save the changes?", "Save", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (confirm.Equals(DialogResult.OK))
                {
                    if (!UserManager.ModifyUser(user, LastIdUser))
                    {
                        MessageBox.Show("The changes where not made");
                    }
                    else
                    {
                        try
                        {
                            File.Copy(pictureBoxPicture.ImageLocation, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", pictureValid)));
                        }
                        catch (Exception)
                        {

                        }
                        MessageBox.Show("Changes saved successfully");
                    }

                    this.Close();
                }
            }
            else 
            {
                MessageBox.Show("All fields are required");
            }

        }
        private bool ValidatingFields()
        {
            return true;
        }

        private void textBoxCi_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxCi,ValidateFields.CIValidation(textBoxCi.Text));
        }

        private void textBoxFirstName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxFirstName,ValidateFields.FirstNameValidation(textBoxFirstName.Text));
        }

        private void textBoxLastName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxLastName,ValidateFields.LastNameValidation(textBoxLastName.Text));
        }

        private void textBoxAddress_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxAddress,ValidateFields.AddressValidation(textBoxAddress.Text));
        }

        private void textBoxCellPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxCellPhone,ValidateFields.CellPhoneValidation(textBoxCellPhone.Text));
        }

        private void textBoxPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxPhone,ValidateFields.PhoneValidation(textBoxPhone.Text));
        }

        private void textBoxEmail_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyUser.SetError(textBoxEmail,ValidateFields.EmailValidation(textBoxEmail.Text));
        }

        private void buttonLoadPicture_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBoxPicture.ImageLocation = openFile.FileName;
                pictureValid = openFile.SafeFileName;
                
            }
            //else
            //{
            //    pictureBoxPicture.Image = null;
            //}
        }
        
    }
}
